package com.huawei.codelab.slice;

import com.google.gson.Gson;
import com.huawei.codelab.ResourceTable;
import com.huawei.codelab.beans.ResultVO;
import com.huawei.codelab.beans.User;
import com.huawei.codelab.utils.DataBaseUtil;
import com.huawei.codelab.utils.HttpRequestUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;

import java.util.Map;

public class LoginAbilitySlice extends AbilitySlice {
    public static String msg;
    private  Gson gson = new Gson();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_login);

        TextField tf1 =(TextField)findComponentById(ResourceTable.Id_login_name_textfield);
        TextField tf2 =(TextField)findComponentById(ResourceTable.Id_login_pwd_textfield);
        Button submitBtn =(Button)findComponentById(ResourceTable.Id_login_btn);
        submitBtn.setClickedListener(component->{
            TaskDispatcher globalTaskDispatcher =this.getGlobalTaskDispatcher(TaskPriority.DEFAULT);
            globalTaskDispatcher.asyncDispatch(()->{
                String urlString ="http://120.26.194.73:9001/user/login?username="+tf1.getText()+"&password="+tf2.getText();
                String s = HttpRequestUtil.sendGetRequest(this, urlString);
                System.out.println("************************************************收到的数据："+s);



                ResultVO resultVO = gson.fromJson(s, ResultVO.class);
                System.out.println("--------------------------------");
                System.out.println(resultVO);
                if(resultVO.getCode()==10000){
                    //将token存入本地数据库
                    int i = DataBaseUtil.setValue("token", resultVO.getMsg(), this);
                     msg = resultVO.getMsg();

//                    Map data =(Map)resultVO.getData();
                    //获取的vo的data部分：存入本地
                    String jsonStr = gson.toJson(resultVO.getData());
                    User user =gson.fromJson(jsonStr,User.class);
                    System.out.println("tttttttttttttttttttttttttttttttttttt图片地址user.getUserImgAddress() = " + user.getUserImgAddress());
                    DataBaseUtil.setValue("userId",user.getUserId()+"",this);
                    DataBaseUtil.setValue("userName",user.getUserName()+"",this);
                    DataBaseUtil.setValue("userFamily",user.getUserFamily()+"",this);
                    DataBaseUtil.setValue("userCall",user.getUserCall()+"",this);
                    DataBaseUtil.setValue("userImgAddress",user.getUserImgAddress()+"",this);

//                    跳转
                    present(new MainAbilitySlice_max(),new Intent());
                }
//                else{
//                    new ToastDialog(this).setText("账号密码错误！").show();
//                }
            });
        });
        Text loginByFace = (Text)findComponentById(ResourceTable.Id_login_face);
        loginByFace.setClickedListener(component -> {
            this.present(new MainAbilitySlice_face(),new Intent());
        });
    }
}
